﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.IoC;
using ProjectResistance.Shell.Views;
using ProjectResistance.Shell.Properties;
using System.Diagnostics;

namespace ProjectResistance.Shell
{
    public partial class MainForm : DoubleBufferedForm, IBackgroundPaintProvider
    {
        [EventPublication(EventNames.DPadEvent)]
        public event KeyEventHandler DPadKeyPressed;

        public Image BackgroundImage { get; set; }
        private Rectangle BackgroundImageExtents { get; set; }
        
        public MainForm()
        {
            InitializeComponent();

            RootWorkItem.Items.Add(resistorWorkspace, WorkspaceNames.ResistorWorkspace);
            RootWorkItem.Items.Add(numericWorkspace, WorkspaceNames.NumericWorkspace);

            var resistorView = RootWorkItem.Items.AddNew<ResistorView>(ViewNames.ResistorView);
            resistorWorkspace.Show(resistorView);

            var numericView = RootWorkItem.Items.AddNew<NumericView>(ViewNames.NumericView);
            numericWorkspace.Show(numericView);

            this.KeyDown += new KeyEventHandler(MainForm_KeyDown);

        }

        void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.Down:
                case Keys.Left:
                case Keys.Right:
                case Keys.Enter:
                    RaiseDPadKeyPressed(e);
                    break;
            }
        }

        private void RaiseDPadKeyPressed(KeyEventArgs e)
        {
            KeyEventHandler handler = DPadKeyPressed;
            if (handler == null) return;

            handler(this, e);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            SetupBackground();
        }

        private void SetupBackground()
        {
            int bgWidth = Math.Min(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Bitmap bm = null;
            if (bgWidth >= 480)
                bm = Resources.Background480x640;
            else
                bm = Resources.Background240x320;
            m_gBuffer.DrawImage(bm, new Rectangle(0, 0, Width, Height), new Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel);
        }

        public void PaintBackground(Graphics g, Rectangle targetRect, Rectangle sourceRect)
        {
            g.DrawImage(m_bmBuffer, targetRect, sourceRect, GraphicsUnit.Pixel);
            //Debug.WriteLine(string.Format("Drawing {0}{1}:{2},{3} to {4}{5}:{6},{7}",
            //    BackgroundImageExtents.X, BackgroundImageExtents.Y, BackgroundImageExtents.Width, BackgroundImageExtents.Height,
            //    ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height));
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuCalculate_Click(object sender, EventArgs e)
        {
            
        }
    }
}